﻿/*
 *  This file is part of Worker Time.
 *
 *  Worker Time is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Worker Time is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Worker Time.  If not, see <http://www.gnu.org/licenses/>.
 */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WorkerTime.Database;
using System.Data.SQLite;
using System.Globalization;

namespace WorkerTime
{
    public partial class GenericManagerGUI : Form
    {
        private Manager database;
        private String table;
        private String projectID;
        private String fields;
        private SQLiteConnection connection;
        private SQLiteCommand command;
        private SQLiteDataAdapter dataAdapter;
        private DataSet dataSet;
        private SQLiteCommandBuilder commandBuilder;
        private ComponentResourceManager localization;

        public GenericManagerGUI(Manager database, String title, String table, String fields)
        {
            localization = new System.ComponentModel.ComponentResourceManager(typeof(WorkerTimeStrings));
            this.database = database;
            this.table = table;
            this.fields = fields;
            InitializeComponent();
            this.Text = localization.GetString("String18") + " " + title.ToLower();
        }

        public GenericManagerGUI(Manager database, String title, String table, String fields, String projectID)
        {
            localization = new System.ComponentModel.ComponentResourceManager(typeof(WorkerTimeStrings));
            this.database = database;
            this.table = table;
            this.fields = fields;
            InitializeComponent();
            this.Text = localization.GetString("String18") + " " + title.ToLower();
            this.projectID = projectID;
        }

        private void ProjectManager_Load(object sender, EventArgs e)
        {
            connection = new SQLiteConnection("Data source=" + database.fileName);
            command = new SQLiteCommand(connection);
            if (table.ToLower().Equals("activity"))
                command.CommandText = "SELECT ID," + fields + " FROM " + table + " WHERE ProjectID='" + projectID + "'";
            else
                command.CommandText = "SELECT ID," + fields + " FROM " + table;

            dataAdapter = new SQLiteDataAdapter(command);
            dataSet = new DataSet(this.table);
            dataSet.Locale = System.Globalization.CultureInfo.InvariantCulture;
            commandBuilder = new SQLiteCommandBuilder(dataAdapter);
            commandBuilder.ConflictOption = ConflictOption.OverwriteChanges;
            dataAdapter.Fill(dataSet, this.table);
            dataAdapter.InsertCommand = commandBuilder.GetInsertCommand();
            Console.Out.WriteLine(commandBuilder.GetInsertCommand().CommandText);
            dataAdapter.DeleteCommand = commandBuilder.GetDeleteCommand();
            Console.Out.WriteLine(commandBuilder.GetDeleteCommand().CommandText);
            dataAdapter.UpdateCommand = commandBuilder.GetUpdateCommand();
            Console.Out.WriteLine(commandBuilder.GetUpdateCommand().CommandText);

            dataGridView1.DataSource = dataSet.Tables[0];
            dataGridView1.Columns["ID"].Visible = false;
            if (table.ToLower().Equals("activity"))
            {
                dataGridView1.Columns["ProjectID"].Visible = false;
            }
            dataGridView1.Columns["Name"].HeaderText = localization.GetString("String23");
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            dataAdapter.Update(dataSet.Tables[0]);
        }

        private void dataGridView1_DefaultValuesNeeded(object sender, DataGridViewRowEventArgs e)
        {
            if (table.ToLower().Equals("activity"))
                e.Row.Cells["ProjectID"].Value = this.projectID;
        }
    }
}
